package com.platform.modules.quality.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.util.StringUtil;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.StringUtils;
import com.platform.entity.Attachment;
import com.platform.entity.Document;
import com.platform.modules.quality.dao.QlyInspectionItemDao;
import com.platform.modules.quality.entity.QlyInspectionItemEntity;
import com.platform.modules.quality.service.QlyInspectionItemService;
import com.platform.modules.quality.service.QlyInspectionService;
import com.platform.modules.quality.vo.QlyInspectionItemListByAppVo;
import com.platform.util.DocUtil;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class QlyInspectionItemServiceImpl extends ServiceImpl<QlyInspectionItemDao, QlyInspectionItemEntity> implements QlyInspectionItemService {

	@Autowired
	QlyInspectionService qlyInspectionService;
    @Autowired
    private DocUtil docUtil;
	
	
//	@Override
//    public boolean save(QlyInspectionItemEntity entity) {
//		if(StringUtils.isNullOrEmpty(entity.getInsItemId())) {
//			qlyInspectionService.updateQueNum(entity.getInsId(), 1);
//		}
//		return this.saveOrUpdate(entity);
//	}


	@Override
	public List<QlyInspectionItemEntity> queryInspectionItemList(String insId) {
		List<QlyInspectionItemEntity> list = baseMapper.queryInspectionItemList(insId);
		if(null!=list && list.size()>0) {
			for(QlyInspectionItemEntity qlyInspectionItem : list) {
				//增加问题图片集合
				List<Attachment> atts = docUtil.getAttListByAttBusinessId(qlyInspectionItem.getInsItemId());
				qlyInspectionItem.setInsAtts(atts);
			}
		}		
		return list;
	}


	@Override
	public List<QlyInspectionItemEntity> queryInspectionItemAndReformList(String insId) {
		//1.因为一个问题可能对多次整改, 这里只查询复查状态 通过的整改
		List<QlyInspectionItemEntity> list = baseMapper.queryInspectionItemAndReformList(insId);
		if(null!=list && list.size()>0) {
			for(QlyInspectionItemEntity qlyInspectionItem : list) {
				//增加问题图片集合
				List<Attachment> atts = docUtil.getAttListByAttBusinessId(qlyInspectionItem.getInsItemId());
				qlyInspectionItem.setInsAtts(atts);
				//增加整改图片集合
				String reformId = qlyInspectionItem.getInsItemReformId();
				if(StringUtil.isNotEmpty(reformId)) {
					List<Attachment> reformAtts = docUtil.getAttListByAttBusinessId(reformId);
					qlyInspectionItem.setRefAtts(reformAtts);
				}
			}
		}		
		return list;
	}
	
	@Override
	public List<QlyInspectionItemEntity> queryInspectionItemUnReformList(String insId, String insItemId){
		//判断质量巡查问题记录是否存在
		QueryWrapper<QlyInspectionItemEntity> queryWrapper = new QueryWrapper<QlyInspectionItemEntity>()
		.eq("INS_ID", insId)
		.eq("INS_ITEM_ID", insItemId);
		QlyInspectionItemEntity qlyInspectionItemEntity = baseMapper.selectOne(queryWrapper);
		if(null==qlyInspectionItemEntity) {
			throw new BusinessException("质量巡查问题记录未找到");
		}
		
		return baseMapper.queryInspectionItemUnReform(insId, insItemId);
	}


	@Override
	public List<QlyInspectionItemEntity> queryInspectionItemUnReviewList(String insId) {
		return baseMapper.queryInspectionItemUnReview(insId);
	}


	@Override
	public List<QlyInspectionItemListByAppVo> queryInspectionItemAndReformListByApp(String insId) {
		List<QlyInspectionItemListByAppVo> qlyInspectionItemListByAppVo = baseMapper.queryInspectionItemAndReformListByApp(insId);
		
		if(null!=qlyInspectionItemListByAppVo && qlyInspectionItemListByAppVo.size()>0) {
			for(int i=0; i<qlyInspectionItemListByAppVo.size(); i++) {
				//增加问题图片集合
				QlyInspectionItemListByAppVo vo = qlyInspectionItemListByAppVo.get(i);
				List<Attachment> atts = docUtil.getAttListByAttBusinessId(vo.getInsItemId());
				vo.setAtts(atts);
				//增加整改图片集合
				String reformId = vo.getInsItemReformId();
				if(StringUtil.isNotEmpty(reformId)) {
					List<Attachment> reformAtts = docUtil.getAttListByAttBusinessId(reformId);
					vo.setReformAtts(reformAtts);
				}
			}
		}
		return qlyInspectionItemListByAppVo;
	}
	
}

